package com.cyys.modules.generator.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cyys.common.ql.AR;
import com.cyys.common.ql.JSONData;
import com.cyys.common.validate.Detail;
import com.cyys.common.validate.Edit;
import com.cyys.modules.core.controller.BaseAction;
import com.cyys.modules.core.sysParam.ParamBasicUtil;
import com.cyys.modules.core.sysParam.ParamConstants;
import com.cyys.modules.generator.model.GeneratorTable;
import com.cyys.modules.generator.service.GeneratorTableService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("/generator/table")
public class GeneratorTableAction extends BaseAction {

	@Resource
	private GeneratorTableService generatorTableService;

	@GetMapping("/list")
	@RequiresPermissions("generator:table:menu")
	public String list(Model model) {
		return "module/generator/table/generatorTable-list";
	}

	@GetMapping("/edit/input")
	@RequiresPermissions("generator:table:edit")
	public String updateInput(@Validated(Detail.class) GeneratorTable generatorTable, Model model) {
		model.addAttribute("modelInheritsMap", ParamBasicUtil.getParamBasicTypeMap().get(ParamConstants.MODEL_INHERITS));
		model.addAttribute("pageModelMap", ParamBasicUtil.getParamBasicTypeMap().get(ParamConstants.PAGE_MODEL));
		model.addAttribute("needFileMap", ParamBasicUtil.getParamBasicTypeMap().get(ParamConstants.GENERATOR_NEED_FILE));
		model.addAttribute("generatorTable", generatorTableService.getById(generatorTable.getId()));
		return "module/generator/table/generatorTable-input";
	}
	
	@ResponseBody
	@PostMapping("/edit/save")
	@RequiresPermissions(value = "generator:table:edit",logical=Logical.OR)
	public AR edit(@Validated(Edit.class) GeneratorTable generatorTable) {
		return generatorTableService.update(generatorTable);
	}


	@ResponseBody
	@PostMapping("/list/listInfo")
	@RequiresPermissions("generator:table:list")
	public String listInfo(GeneratorTable generatorTable, Page page) {
		return JSONData.toLayerData(generatorTableService.list(page , new QueryWrapper(generatorTable),null));
	}


	@ResponseBody
	@PostMapping("/del/deleteById")
	@RequiresPermissions("generator:table:del")
	public AR deleteById(String id){
		return generatorTableService.deleteById(id);
	}

	@ResponseBody
	@PostMapping("/del/deleteByIds")
	@RequiresPermissions("generator:table:del")
	public AR deleteByIds(String ids) {
		return generatorTableService.deleteBatchIds(ids);
	}


	@ResponseBody
	@PostMapping("/createAllTable")
	@RequiresPermissions("generator:table:create")
	public AR createAllTable() {
		return generatorTableService.createAllTable();
	}

	@ResponseBody
	@PostMapping("/reloadTable")
	@RequiresPermissions("generator:table:create")
	public AR reloadTable(String id) {
		return generatorTableService.reloadTable(id);
	}

	@ResponseBody
	@PostMapping("/generatorCreate")
	@RequiresPermissions("generator:table:create")
	public AR generatorCreate(String id) throws Exception{
		return generatorTableService.generatorCreate(id);
	}

	@ResponseBody
	@PostMapping("/generatorDelete")
	@RequiresPermissions("generator:table:create")
	public AR generatorDelete(String id) throws Exception{
		return generatorTableService.generatorDelete(id);
	}
}
